package sybel.pan;

import javax.swing.JPanel;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import sybel.DOMPanelInterface;

/**
 *
 * @author Stefano
 */
public class Trigger extends javax.swing.JPanel implements DOMPanelInterface {

    /** Creates new form Trigger */
    public Trigger(Node triggerNode) {
        trigger = (Element)triggerNode;
        initComponents();
        initPanel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCBInterHappen = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();

        jCBInterHappen.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose one...", "Interaction", "Happening" }));
        jCBInterHappen.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBInterHappenItemStateChanged(evt);
            }
        });

        jScrollPane1.setBorder(null);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 609, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 402, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCBInterHappen, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(490, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCBInterHappen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jCBInterHappenItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBInterHappenItemStateChanged
        TriggerSelector(jCBInterHappen.getSelectedIndex());
}//GEN-LAST:event_jCBInterHappenItemStateChanged

    /**
     * Metodo che inizializza il pannello principale di trigger nel caso in cui
     * il nodo <trigger> non sia vuoto
     */
    public void initPanel(){
        NodeList nl = trigger.getChildNodes();
        if(nl.getLength() != 0){
            if(nl.item(0).getNodeName() == "interaction"){
                jCBInterHappen.setSelectedIndex(1);
                TriggerSelector(1);
            }else{
                jCBInterHappen.setSelectedIndex(2);
                TriggerSelector(2);
            }
        }
    }


    /*  Metodo che carica i pannelli happening o interaction dipendentemente dal
     *  cambiamento della selezione nella combobox di trigger
     */
    public void TriggerSelector(int selection){
        switch(selection){
            case 1:
                jPanel1.removeAll();
                JPanel interaction = new Interaction(trigger);

                setTriggerPanel(interaction);
                break;

            case 2:
                jPanel1.removeAll();
                JPanel happening = new Happening(trigger);

                setTriggerPanel(happening);
                break;

            default:
                jPanel1.removeAll();
                jPanel1.repaint();
                break;
        }
    }

    /**
     * Metodo che imposta il pannello 1 con l'entità passata per parametro
     * @param entity - Interaction o Happening da inserire nel jPanel1
     */
    private void setTriggerPanel(JPanel entity){
        /* Imposto il layout inserendo il pannello Action Execute nel pannello
         * adibito ad ospitare i componenti scelti nella combobox   */
        jPanel1.removeAll();
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(entity, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(entity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                );

        // rendo visibile il nuovo pannello
        jPanel1.repaint();
        jPanel1.setVisible(true);
    }

    /**
     * Metodo che salva tutti i dati presenti nel pannello creando il nodo xml <trigger>
     * e riempiendolo dei dati presenti in esso
     * @return il nodo trigger riempito
     */
    public Node saveData(){
        if(trigger.hasChildNodes()){
            trigger.removeChild(trigger.getFirstChild());
        }
        if(jCBInterHappen.getSelectedIndex() != 0){
            //if(jPanel1.getComponent(0).toString().contains("Interaction")){
                //Interaction inter = (Interaction)jPanel1.getComponent(0);
                trigger.appendChild(((DOMPanelInterface)jPanel1.getComponent(0)).saveData());
            //}else{
            //    Happening happ = (Happening)jPanel1.getComponent(0);
            //    trigger.appendChild(happ.saveData());
            //}
        }
        return trigger;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jCBInterHappen;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    private Element trigger;
}
